const mysql = require('mysql2/promise');
const fs = require('fs').promises;
const path = require('path');

async function initDatabase() {
    try {
        // 读取 SQL 文件
        const sqlContent = await fs.readFile(
            path.join(__dirname, '../database/init.sql'),
            'utf8'
        );

        // 创建数据库连接
        const connection = await mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: '',
            database: 'db',
            multipleStatements: true
        });

        // 执行 SQL 语句
        await connection.query(sqlContent);
        console.log('数据库初始化成功');
        
        await connection.end();
    } catch (error) {
        console.error('数据库初始化失败:', error);
        process.exit(1);
    }
}

initDatabase(); 